package com.g2room.myuall.grap.ky;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.grap.GenericGrap;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.ky.model.po.PeeGao;
import com.g2room.myuall.grap.ky.model.po.PeeStudents;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapKyStudents {
//	考研kaoyan.com
//	留学http://www.liuxue86.com/
//	论文http://biyelunwen.yjbys.com/
	private static final String source="考研帮"; 
	GrapKyStudents() {}
	public static void main(String[] args) throws Exception{
//		new GrapKyStudents().getPageStudent();
		new GrapKyStudents().getUrls();
	}
	
	private void getUrls() {
		String url = "http://yz.kaoyan.com/pku/fenshuxian/";
		// 获取分页信息
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Elements aels = doc.getElementsByAttributeValue("class", "subGuideList").first().getElementsByTag("a");
		for(Element ael : aels) {
			System.out.println("infosMap.put(\""+ael.attr("href")+"/\", \""+ael.text()+"\");");
		}
	}
	
	/**
	 * 获取分页列表
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getPageStudent() {
		String url = "http://www.kaoyan.com/zhaosheng/";
		List<PeeGao> pgs = new ArrayList<PeeGao>();
		// 获取分页信息
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setDescription("获取Page错误");
			insert2ErrDb(ge);
		}
		if(doc == null) return;
		Elements liEls = doc.getElementsByAttributeValue("class", "zsSelectArea").first().getElementsByTag("li");
		Map<String, String> urls = new HashMap<String, String>();
		for(Element liEl : liEls) {
			if(liEl.text().contains("全部")) continue;
			url = "http://www.kaoyan.com/zhaosheng/" + liEl.getElementsByTag("a").attr("href").replace("./", "");
			urls.put(url, liEl.text());
		}
        for(String listUrl : urls.keySet()) {
        	getListStudent(listUrl, urls.get(listUrl));
        }
	}
	

	/**
	 * 获取List列表
	 * @param url
	 */
	public void getListStudent(String url, String province) {
		// 获取分页信息
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("2");
			ge.setDescription("获取List错误");
			insert2ErrDb(ge);
		}
		if(doc == null) return;
		Elements liEls = doc.getElementsByAttributeValue("class", "list areaZslist").first().getElementsByTag("li");
		/**
		 * <li><span class="fr"></span>
		 * <a href="http://yz.kaoyan.com/hnrku/zhaosheng/5812f93fb5b63.html" target="_blank" title="2017年湖南人文科技学院“退役大学生士兵计划”考研招生公告">2017年湖南人文科技学院“退役大学生士兵计划”考研招生公告</a></li>
		 */
		List<PeeStudents> pses = new ArrayList<PeeStudents>();
		List<String> detailUrls = new ArrayList<String>();
		for(Element liEl : liEls) {
			detailUrls.add(liEl.getElementsByTag("a").first().attr("href"));
		}
        for(String detailUrl : detailUrls) {
        	getDetailStudents(detailUrl);
        }
	}
	
	/**
	 * 获取详情数据
	 */
	public boolean getDetailStudents(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
//			e.printStackTrace();
			System.out.println(url);
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setDescription("获取Detail Student错误");
			insert2ErrDb(ge);
		}
		if(doc == null) return false;
        return true;
	}
	
	public String getDetailGaoYuanxi(String str) {
		String url = "";
		Document doc = null;
		try {
			doc = GenericGrap.getDocByHtml(str);
			Element el = doc.getElementsByTag("a").first();
			url = el.attr("href");
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			System.out.println(url);
//			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setContent(str);
			ge.setDescription("获取院系联系方式错误");
			insert2ErrDb(ge);
		}
		if(doc == null) return null;
		Elements aels = doc.getElementsByTag("table");
		return aels.size() > 0 ? aels.first().outerHtml() : null;
	}
	

	public static void insert2GaoDb(PeeGao pg) {
		try {
			PreparedStatement ps = DbUtil.getConn().prepareStatement("insert into pee_gao(province_name,source_url,address,area,email,source,phone,zip_code,faculty,fax,content,school_name) values(?,?,?,?,?,?,?,?,?,?,?,?)");
			//province_name,source_url,address,area,email,source,phone,zip_code,faculty,fax,content,school_name
			ps.setString(1, pg.getProvinceName());
			ps.setString(2, pg.getSourceUrl());
			ps.setString(3, pg.getAddress());
			ps.setString(4, pg.getArea());
			ps.setString(5, pg.getEmail());
			ps.setString(6, pg.getSource());
			ps.setString(7, pg.getPhone());
			ps.setString(8, pg.getZipCode());
			ps.setString(9, pg.getFaculty());
			ps.setString(10, pg.getFax());
			ps.setString(11, pg.getContent());
			ps.setString(12, pg.getSchoolName());
			ps.execute();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert2ErrDb(GrapErr ge) {
		try {
			PreparedStatement grapPs = DbUtil.getConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Map<String, Integer> getErrs() {
		Map<String, Integer> errMap = new HashMap<String, Integer>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select url,type from univ_err where type=2";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				errMap.put(rs.getString("url"), rs.getInt("type"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return errMap;
	}
}
